package com.cy.jt.security.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService{
//按类型查找，按名字查找，（从容器中查找bean），然后注入，
 @Autowired
 private BCryptPasswordEncoder passwordEncoder;

 @Override
 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
 if(!"jack".equals(username))
  throw new UsernameNotFoundException("user not exists");
  String encodedPwd=passwordEncoder.encode("123456");
  List<GrantedAuthority> authorities=AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_jinpai,ROLE_tongpai,sys:res:retrieve,sys:res:create");
  User user=new User(username,encodedPwd,authorities);
  return user;
 }
}
